<?php

header("Content-Type: text/html;charset=utf8");
require_once __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "dist" . DIRECTORY_SEPARATOR . "oss" . DIRECTORY_SEPARATOR . "samples" . DIRECTORY_SEPARATOR . "Common.php";

use OSS\OssClient;
use OSS\Core\OssException;

class ResourceDb {

    private $mysql;

    public function __construct(InMysql $mysql = null) {
        if ($mysql == null) {
            $this->mysql = new InMysql();
        } else {
            $this->mysql = $mysql;
        }
    }

    /**
     * 资源列表
     * @param type $cs
     * @param type $type
     * @param type $page
     * @param type $pageSize
     * @return type
     */
    public function listResource($cs, $page, $pageSize) {
        $start = $page * $pageSize;
        $where = " where rid > 0 ";
        if ($cs > -1) {
            $where .= " and Cs = " . $cs;
        }

        $sql = "select * from res union select * from resm " . $where . " limit " . $start . "," . $pageSize;
        $sql2 = "select * from res union select * from resm " . $where;
        $data['data'] = $this->mysql->fetchData($sql);
        $data['count'] = count($this->mysql->fetchData($sql2));

        return $data;
    }

    /**
     *
     * @param type $ossClient
     * @param type $bucket
     * @param type $dir
     * @param type $cs
     * @return type
     */
    function getObjectMeta($ossClient, $bucket, $dir, $cs) {
        try {
//        $prefix = 'res/1/';
            $prefix = $dir . "/" . $cs . "/";
            $delimiter = '';
            $nextMarker = '';
            $maxkeys = 1000;
            $options = array(
                'delimiter' => $delimiter,
                'prefix' => $prefix,
                'max-keys' => $maxkeys,
                'marker' => $nextMarker,
            );
            $objectAcl = $ossClient->listObjects($bucket, $options);
        } catch (OssException $e) {
            $res['code'] = 125;
            $res['msg'] = $e->getMessage();
            return $res;
        }

        $objectList = $objectAcl->getObjectList();
        $file_map = array();
        $data = "";
        if (!empty($objectList)) {
            $i = 0;
            foreach ($objectList as $objectInfo) {
                $array = explode("/", $objectInfo->getKey());
                if (isset($array[2])) {
                    $file_array = explode(".v.", $array[2]);
                    if (isset($file_array[1])) {
                        $file_name = $file_array[0];
                        $version = substr($file_array[1], 0, strpos($file_array[1], ".zip"));
                        if (isset($file_map[$file_name])) {
                            if (floatval($version) > floatval($file_map[$file_name])) {
                                $file_map[$file_name] = $version;
                            }
                        } else {
                            $file_map[$file_name] = $version;
                        }
                    }
                } else {

                }
                if ($objectInfo->getSize() > 0) {
                    $fname = "/" . $cs . "/" . $array[2];
                    $md5 = strtoupper(md5($fname));
                    $cs_data = intval($cs) - 1;
                    $url = $prefix_path = "http://" . Config::OSS_TEST_BUCKET . "." . Config::OSS_ENDPOINT . "/" . $dir . $fname;
                    // $data .= "('$fname','$md5',now(),'$version','$file_name',$cs_data,'$url'),";
                }
                $i++;
            }
        }

        if (count($file_map) == 0) {
            $res['code'] = 124;
            $res['msg'] = "找不到OSS文件";
            return $res;
        }

        while ($value = current($file_map)) {
            $key_file_name = key($file_map);
            $fname = "/" . $cs . "/" . $key_file_name . ".v." . $value . ".zip";
            $md5 = strtoupper(md5($fname));
            $cs_data = intval($cs) - 1;
            $url = $prefix_path = "http://" . Config::OSS_TEST_BUCKET . "." . Config::OSS_ENDPOINT . "/" . $dir . $fname;
            $now = time();
            $data .= "('$fname','$md5',$now,'$value','$key_file_name',$cs_data,'$url'),";
            next($file_map);
        }

        $data_len = strlen($data);
        //检查字符串最后一位是不是逗号，如果是去掉最后一个逗号
        if ($data_len > 0 && substr($data, $data_len - 1, $data_len) == ",") {
            $data = substr($data, 0, $data_len - 1);
        }

        $table = $dir;
        $sql = "replace INTO $table (fname,md5,ctime,ver,rname,Cs,url) VALUES " . $data;
        $sqls = array();
        array_push($sqls, $sql);
        if ($this->mysql->executeBatch($sqls)) {
            $res['code'] = 0;
            $res['msg'] = "";
            return $res;
        }
        $res['code'] = 123;
        $res['msg'] = "扫描刷新资源失败";
        return $res;
    }

}

?>